package com.bsj.power.sdk.web;

import cn.hutool.extra.spring.SpringUtil;
import com.bsj.power.common.mapper.DeviceMapper;
import com.bsj.power.sdk.cache.VideoChCache;
import com.bsj.power.sdk.config.hksdk.HKSDKClientManage;
import com.bsj.power.sdk.service.handle.HKSDKAlarmHandle;
import com.bsj.power.sdk.service.handle.HKSdkHandle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;

/**
 * @author yinhao
 * @version 1.0
 * @description 启动类
 * @date 2024/8/23
 */
@Component
@Slf4j
@Import(SpringUtil.class)
public class Action implements CommandLineRunner {

    @Value("${annexFile.channel}")
    private String channelPath;

    @Autowired
    private HKSdkHandle hkSdkHandle;

    @Autowired
    private HKSDKAlarmHandle hksdkAlarmHandle;

    @Resource
    private DeviceMapper deviceMapper;

    @Override
    public void run(String... args) throws Exception {
        VideoChCache videoChCache = new VideoChCache();
        videoChCache.start(channelPath, deviceMapper);
        HKSDKClientManage.init();
        hkSdkHandle.init();
        hksdkAlarmHandle.init();
    }

    /**
     * 销毁使用的方法
     */
    @PreDestroy
    public void destroy() throws Exception {
        HKSDKClientManage.logout();
        hkSdkHandle.stop();
    }
}
